
package com.yishun.springboot.util;

import com.alibaba.fastjson.JSONObject;

/**
 * @author 作者:LuoXingFa
 *
 * @version 创建时间:2022年7月12日{time}
 *
 * @Description 类说明
 */
public class ResultUtils {

	private int code;// 状态码
	private String msg;
	private int count;// 做分页的
	private Object data;

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	// 私有构造
	private ResultUtils() {
	}

	// 查询成功的返回
	public static ResultUtils getSuccess(String msg, Object data) {
		ResultUtils resultUtils = new ResultUtils();
		resultUtils.setMsg(msg);
		resultUtils.setData(data);
		return resultUtils;
	}

	// 增删改成功后没有返回值
	public static ResultUtils getSuccess(String msg) {
		return ResultUtils.getSuccess(msg, null);
	}

	// 失败后的方法
	public static ResultUtils getFail(String msg) {
		ResultUtils resultUtils = new ResultUtils();
		resultUtils.setCode(500);
		resultUtils.setMsg(msg);
		return resultUtils;
	}

	public static ResultUtils getFail(String msg, Integer code) {
		ResultUtils resultUtils = new ResultUtils();
		resultUtils.setCode(code);
		resultUtils.setMsg(msg);
		return resultUtils;
	}

	public static JSONObject jsonFail(String msg) {
		JSONObject res = new JSONObject();
		res.put("code", 500);
		res.put("msg", msg);
		return res;
	}

	public static JSONObject jsonFail(String msg, Integer code) {
		JSONObject res = new JSONObject();
		res.put("code", code);
		res.put("msg", msg);
		return res;
	}

	public static JSONObject jsonFail() {
		JSONObject res = new JSONObject();
		res.put("code", 500);
		res.put("msg", "fail");
		return res;
	}

	public static JSONObject jsonFail(Integer code) {
		JSONObject res = new JSONObject();
		switch (code) {
		case 300:
			res.put("code", 300);
			res.put("msg", "权限不足");
			break;
		case 400:
			res.put("code", 400);
			res.put("msg", "必要参数为空");
			break;
		case 405:
			res.put("code", 405);
			res.put("msg", "图片为空");
			break;
		case 410:
			/**
			 * 用于修改的时候判断，如果未修改任何内容无法修改
			 */
			res.put("code", 410);
			res.put("msg", "参数为空");
			break;
		case 420:
			res.put("code", 420);
			res.put("msg", "图片删除失败");
			break;
		case 425:
			res.put("code", 425);
			res.put("msg", "个人信息有误，请联系管理员");
			break;
		case 430:
			res.put("code", 430);
			res.put("msg", "该数据被引用，无法删除");
			break;
		case 505:
			res.put("code", 505);
			res.put("msg", "程序异常，请联系管理员");
			break;
		default:
			res.put("code", 0000);
			res.put("msg", "没有改错误码");
			break;
		}

		return res;
	}

	public static JSONObject jsonSuccess(String msg, Object data) {
		JSONObject res = new JSONObject();
		res.put("code", 200);
		res.put("msg", msg);
		res.put("data", data);
		return res;
	}

	public static JSONObject jsonSuccessNotData(String msg) {
		JSONObject res = new JSONObject();
		res.put("code", 200);
		res.put("msg", msg);
		return res;
	}

	public static JSONObject jsonSuccess(Object data) {
		JSONObject res = new JSONObject();
		res.put("code", 200);
		res.put("data", data);
		return res;
	}

	public static JSONObject jsonSuccess(Object data, Integer totalPage, Integer count) {
		JSONObject res = new JSONObject();
		res.put("code", 200);
		res.put("data", data);
		res.put("totalPage", totalPage);
		res.put("count", count);
		return res;
	}

	public static JSONObject jsonSuccess() {
		JSONObject res = new JSONObject();
		res.put("code", 200);
		res.put("msg", "succeed");
		return res;
	}

}
